package org.timeticker.impl;

import org.timeticker.Activity;
import org.timeticker.Day;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Default activity implementation
 *
 * @author Ivan Latysh
 * @version 0.1
 * @since 11-Jul-2007 : 9:45:29 PM
 */
public class ActivityImpl implements Activity {

  /** Activity date */
  protected Day day;
  /** Activity title */
  protected String title;
  /** Work type */
  protected String workTypeCode;
  /** ProjectDialog */
  protected String projectCode;
  /** Dtart time */
  protected Date start = new Date();
  /** Finish time */
  protected Date finish;
  /** Comments */
  protected String comments;
  /** Parameters map */
  protected Map<String, ? extends Serializable> parameters = new HashMap<String, Serializable>();

  /**
   * Create new activity
   */
  public ActivityImpl() {
  }

  /** {@inheritDoc} */
  public Day getDay() {
    return day;
  }

  /** {@inheritDoc} */  
  public void setDay(Day day) {
    this.day = day;
  }

  /** {@inheritDoc} */
  public String getTitle() {
    return title;
  }

  /** {@inheritDoc} */
  public void setTitle(String title) {
    this.title = title;
  }

  public String getWorkTypeCode() {
    return workTypeCode;
  }

  public void setWorkTypeCode(String workTypeCode) {
    this.workTypeCode = workTypeCode;
  }

  public String getProjectCode() {
    return projectCode;
  }

  public void setProjectCode(String projectCode) {
    this.projectCode = projectCode;
  }

  /** {@inheritDoc} */
  public long getDuration() {
    return null!=start
            ?(null!=finish
            ?finish.getTime() - start.getTime()
            :System.currentTimeMillis() - start.getTime())
            :0;
  }

  /** {@inheritDoc} */
  public void start() {
    if (null==start) start = new Date(System.currentTimeMillis());
    if (null!=finish) finish = null;
  }

  /** {@inheritDoc} */
  public Date getStartTime() {
    return start;
  }

  /** {@inheritDoc} */
  public void setStartTime(Date time) {
    this.start = time;
  }


  /** {@inheritDoc} */
  public Date getFinishTime() {
    return finish;
  }

  /** {@inheritDoc} */
  public void setFinishTime(Date time) {
    this.finish = time;
  }

  /** {@inheritDoc} */
  public void stop() {
    finish = new Date(System.currentTimeMillis());
  }

  public String getComments() {
    return comments;
  }

  public void setComments(String comments) {
    this.comments = comments;
  }

  /** {@inheritDoc} */
  public Map<String, ? extends Serializable> getCustomFields() {
    return null;
  }

  /** {@inheritDoc} */
  public final Map<String, ? extends Serializable> getParameters() {
    return parameters;
  }

  /**
   * Return string representation of the activity
   *
   * @return activity string representation
   */
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("[")
            .append("date=").append(day).append("; ")
            .append("title=").append(title).append("; ")
            .append("workTypeCode=").append(workTypeCode).append("; ")
            .append("projectCode=").append(projectCode).append("; ")
            .append("startTime=").append(start).append("; ")
            .append("finishTime=").append(finish).append("; ")
            .append("duration=").append(getDuration()).append("ms.").append("; ")
            .append("comments=").append(comments).append("ms.")
            .append("]");
    return sb.toString();
  }
}
